package com.prs.crm.action.fs;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.Currency;
import com.prs.crm.domain.fs.BankAccount;
import com.prs.crm.service.CurrencyService;
import com.prs.crm.service.fs.BankAccountService;

public class BankAccountAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private BankAccount bankAccount;
	private BankAccountService bankAccountService;
	private PaginationSupport pagination;
	private Integer[] ids;

	private CurrencyService currencyService;

	private Collection<Currency> currencys;
	private Collection<TreeNode> nodes;

	@Action("listBankAccount")
	public String listBankAccount() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				BankAccount.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getBankAccountService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editBankAccount")
	public String editBankAccount() {
		currencys = this.getCurrencyService().getAllCurrency();
		return SUCCESS;
	}

	@Action(value = "loadBankAccountTree", results = { @Result(name = "success", type = "json", params = {
			"root", "nodes" }) })
	public String loadBankAccountTree() {
		nodes = new ArrayList<TreeNode>();
		Collection<BankAccount> regs = this.getBankAccountService()
				.getAllBankAccount();
		for (BankAccount reg : regs) {
			TreeNode node = new TreeNode();
			node.setId(reg.getId().toString());
			node.setText(reg.getName());
			nodes.add(node);
		}
		return SUCCESS;
	}

	@Action(value = "saveBankAccount", results = {
			@Result(name = "success", type = "redirect", location = "listBankAccount"),
			@Result(name = "error", type = "chain", location = "editBankAccount") })
	public String saveBankAccount() {
		if (bankAccount.getName() == null || bankAccount.getAccount() == null) {
			this.addActionError("请确定账号名和账号不能为空");
			return ERROR;
		}
		if (bankAccount.getCurrency() == null) {
			addActionError("币种不能为空");
			return ERROR;
		}
		this.getBankAccountService().save(bankAccount);
		return SUCCESS;

	}

	@Action(value = "removeBankAccount", results = {
			@Result(name = "success", type = "redirect", location = "listBankAccount"),
			@Result(name = "error", type = "chain", location = "listBankAccount") })
	public String removeBankAccount() {
		if (ids == null) {
			this.addActionError("请选择要删除的银行账号");
			return ERROR;
		}
		for (Integer id : ids) {
			BankAccount bankAccount = this.getBankAccountService().get(id);
			if (bankAccount.getDetails().size() != 0) {
				this.addActionError("已有客户引用，不能删除");
				return ERROR;
			}
		}

		this.getBankAccountService().batchRemove(ids);
		return SUCCESS;
	}

	public BankAccount getBankAccount() {
		return bankAccount;
	}

	public void setBankAccount(BankAccount bankAccount) {
		this.bankAccount = bankAccount;
	}

	public BankAccountService getBankAccountService() {
		return bankAccountService;
	}

	public void setBankAccountService(BankAccountService bankAccountService) {
		this.bankAccountService = bankAccountService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public void setCurrencys(Collection<Currency> currencys) {
		this.currencys = currencys;
	}

	public Collection<Currency> getCurrencys() {
		return currencys;
	}

	public void setCurrencyService(CurrencyService currencyService) {
		this.currencyService = currencyService;
	}

	public CurrencyService getCurrencyService() {
		return currencyService;
	}

	public Collection<TreeNode> getNodes() {
		return nodes;
	}

	public void setNodes(Collection<TreeNode> nodes) {
		this.nodes = nodes;
	}

}
